package com.lz.helper.idcard

import android.content.Context
import android.util.Log
import com.zkteco.android.biometric.core.device.ParameterHelper
import com.zkteco.android.biometric.core.device.TransportType
import com.zkteco.android.biometric.module.idcard.IDCardReader
import com.zkteco.android.biometric.module.idcard.IDCardReaderFactory
import com.zkteco.android.biometric.module.idcard.IDCardType
import com.zkteco.android.biometric.module.idcard.exception.IDCardReaderException

//辽源通达串口方式的身份证识别
class SerialIDCardRequest(context: Context, readFinger: Boolean, handler: IDCardHandler) :
    IDCardRequest(context, readFinger, handler) {

    override fun run() {
        Log.d(TAG, "...... SerialIDCardRequest thread start ......")
        openHub()
        val reader = initIDCardReader()
        if (reader == null) {
            handler.sendCardError(ERR_INIT)
            return
        }
        try {
            reader.open(0)
        } catch (e: IDCardReaderException) {
            handler.sendCardError(ERR_OPEN) // open exception
            IDCardReaderFactory.destroy(reader)
            return
        }
        readIDCard(reader)
        IDCardReaderFactory.destroy(reader)
        Log.d(TAG, "......... SerialIDCardRequest quit  .............")
    }

    private fun readIDCard(reader: IDCardReader) {
        var needReFind = true
        while (!isQuit()) {
            await()
            if (isQuit()) break
            if (needReFind) {
                try {
                    reader.findCard(0)
                    reader.selectCard(0)
                } catch (e: IDCardReaderException) {
                    Log.d(TAG, "SerialIDCardRequest findCard err $e")
                    delay(DELAYED_ERR)
                    continue
                }
                try {
                    Thread.sleep(50)
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
            }
            if (isQuit()) break
            try {
                val cardType: Int = reader.readCardEx(0, if (readFinger) 1 else 0) //1表示读指纹
                if (isQuit()) break
                if (cardType == IDCardType.TYPE_CARD_SFZ || cardType == IDCardType.TYPE_CARD_GAT) {
                    needReFind = false
                    val idCardInfo = reader.lastIDCardInfo
                    if (idCardInfo != null) {
                        handler.sendCardResult(IDCardInfo(idCardInfo))
                        delay(DELAYED_SUCCESS)
                    }
                } else {
                    needReFind = true
                    delay(DELAYED_ERR)
                }
            } catch (e: IDCardReaderException) {
                Log.d(TAG, "SerialIDCardRequest readCarEx err $e")
            }
        }
        try {
            reader.close(0)
            Log.d(TAG, "SerialIDCardRequest close IDCardReader ")
        } catch (e: IDCardReaderException) {
            e.printStackTrace()
        }
    }

    //初始化IDCardReader
    private fun initIDCardReader(): IDCardReader? {
        val params: HashMap<String, Any> = HashMap()
        params[ParameterHelper.PARAM_SERIAL_SERIALNAME] = "/dev/ttyS1"
        params[ParameterHelper.PARAM_SERIAL_BAUDRATE] = 115200
        return IDCardReaderFactory.createIDCardReader(context, TransportType.SERIALPORT, params)
    }
}